$(function () {
    // 解决登录页面可能会在iframe标签中显示的问题
    if (window.top !== window) {
        window.top.location = window.location;
    }
    layui.use(['layer'], function () {
        var layer = layui.layer;
        //事件提交
        $("#sum").click(function () {
            var email = $("#email").val();
            var password = $("#password").val();
            // 正则表达式
            var emailz = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            var passwordz = /[\S]+/;
            if (!emailz.test(email)) {
                layer.msg('邮箱格式不正确', {
                    icon: 2,
                    time: 1000
                });
                return;
            } else if (!passwordz.test(password)) {
                layer.msg('必填项不能为空', {
                    icon: 2,
                    time: 1000
                });
                return;
            } else {
                $.ajax({
                    type: 'get',
                    url: 'http://api.hk.com/admin/tologin',
                    // 跨域携带Cookie
                    xhrFields: {
                        withCredentials: true
                    },
                    crossDomain: true,
                    data: $("#adminForm").serializeArray(),
                    dataType: 'json',
                    success: function (res) {
                        if (res.message == "success") {
                            layer.msg('登录成功！', {
                                icon: 1,
                                time: 1200,
                                end: function () {
                                    location.href = '/html/admin/index.html';
                                }
                            })
                            return;
                        }
                        layer.msg('账户或密码错误，请重试！', {
                            icon: 2,
                            time: 1200
                        });
                        return;
                    },
                    error: function () {
                        layer.msg('请求接口异常！', {
                            icon: 0,
                            time: 1500
                        });
                        return;
                    }
                });
                return;
            }
        })
    });
})